package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.MyRequiresAdapter;
import cn.hrlly.tao2shou.domain.RequireList;
import cn.hrlly.tao2shou.domain.Requires;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.MyRequireListPresenter;
import cn.hrlly.tao2shou.presenter.MyRequireListPresenterImpl;
import cn.hrlly.tao2shou.utils.NetUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.MyRequireListView;

public class MyRequireActivity extends BaseActivity implements MyRequireListView{

    @Bind(R.id.lvRequires)
    ListView lvRequires;
    private MyRequireListPresenter myRequireListPresenter;
    private ArrayList<Requires> list;
    private MyRequiresAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_require);
        //绑定控件
        ButterKnife.bind(this);
        //初始化
        init();
        //点击事件
        clickListview();
    }

    //初始化
    public void init(){
        myRequireListPresenter=new MyRequireListPresenterImpl(this,this);
        list=new ArrayList<Requires>();
        if(NetUtil.ping(this)){
            myRequireListPresenter.showMyRequireList();
        }else {
            myRequireListPresenter.showCacheMyRequireList();
        }
    }

    //listviewd的点击事件
    public void clickListview(){
        lvRequires.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MyRequireActivity.this,MyRequireDetailsActivity.class);
                intent.putExtra(Config.key_require_id,list.get(position).getRequireId());
                startActivity(intent);
            }
        });
    }

    //以下是对activity的操作
    @Override
    public void showListView(RequireList requireList) {
        list.addAll(requireList.getRequires());
        adapter=new MyRequiresAdapter(list,this);
        lvRequires.setAdapter(adapter);
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT);
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT);
    }
}
